/*
 * persistencia.c
 *
 *  Created on: 06/11/2011
 *      Author: utn_so
 */

#include "persistencia.h"

extern unsigned char* bootAndFats;
extern pthread_mutex_t mutex_asin_arch_disco;
extern FILE* archivoDisco;

int32_t persistirFAT(struct bootSector* boot)
{

	uint32_t sectoresPrincipales = firstDataSector(boot);
	sectoresPrincipales -= 1;

	uint32_t largoBuff = boot -> BPB_BytsPerSec * sectoresPrincipales;
	int i;

	pthread_mutex_lock(&mutex_asin_arch_disco);
	fseek(archivoDisco, 0, SEEK_SET);

	for(i=0; i<largoBuff; i++)
		fputc(bootAndFats[i], archivoDisco);

	pthread_mutex_unlock(&mutex_asin_arch_disco);

	return 0;

}

int32_t persistirCluster(struct bootSector* boot, uint32_t cluster, unsigned char* clusterBuffer)
{

	uint32_t sector = firstSectorofCluster(boot, cluster);

	uint32_t largoCluster = boot -> BPB_BytsPerSec * boot -> BPB_SecPerClus;
	uint32_t aux1 = sector * boot -> BPB_BytsPerSec;
	int i;

	pthread_mutex_lock(&mutex_asin_arch_disco);
	fseek(archivoDisco, aux1, SEEK_SET);

	for(i=0; i<largoCluster; i++)
		fputc(clusterBuffer[i], archivoDisco);

	pthread_mutex_unlock(&mutex_asin_arch_disco);

	return 0;

}
